﻿using Kingdee.BOS.App.Data;
using Kingdee.BOS.Core.Bill.PlugIn;
using Kingdee.BOS.Core.DynamicForm.PlugIn.Args;
using Kingdee.BOS.Orm.DataEntity;
using Kingdee.BOS.Orm.Metadata.DataEntity;
using Kingdee.BOS.Util;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RK.FormPlugin.XHX
{
    [Description("报关单套打插件"), HotUpdate]
    public class TaoDaFaPiao : AbstractBillPlugIn
    {
        public override void OnPrepareNotePrintData(PreparePrintDataEventArgs e)
        {
            base.OnPrepareNotePrintData(e);
            if (e.DataSourceId.EqualsIgnoreCase("FBillHead"))
            {
                if (e.DataObjects != null)
                {
                    DynamicObjectType dot = e.DataObjects.FirstOrDefault().DynamicObjectType;
                    dot.RegisterSimpleProperty("CTNS", typeof(object), attributes: new SimplePropertyAttribute() { Alias = "CTNS" });
                    dot.RegisterSimpleProperty("SumQty", typeof(object), attributes: new SimplePropertyAttribute() { Alias = "SumQty" });
                    dot.RegisterSimpleProperty("SumAccount", typeof(object), attributes: new SimplePropertyAttribute() { Alias = "SumAccount" });
                    dot.RegisterSimpleProperty("SumNW", typeof(object), attributes: new SimplePropertyAttribute() { Alias = "SumNW" });
                    dot.RegisterSimpleProperty("SumGW", typeof(object), attributes: new SimplePropertyAttribute() { Alias = "SumGW" });
                    dot.RegisterSimpleProperty("Marks", typeof(object), attributes: new SimplePropertyAttribute() { Alias = "Marks" });

                    List<string> ids = new List<string>();
                    foreach (var item in e.DataObjects)
                    {
                        string id = Convert.ToString(item["FID"]);
                        if (!ids.Contains(id))
                        {
                            ids.Add(id);
                        }
                    }
                    if (ids.Count <= 0)
                    {
                        this.View.ShowErrMessage("请先选择数据！");
                        return;
                    }

                    string FId = this.View.Model.DataObject["id"].ToString();

                    string sql = string.Format(@"/*dialect*/ 
                    SELECT SUM(b.F_QTY)sum_qty,SUM(b.F_AMOUNT) sum_account,sum(b.f_ctns) AS ctn,SUM(b.f_gw) gw,
                    SUM(b.f_nw) nw FROM	TRPU_t_Cust100001 a
                    LEFT JOIN TRPU_t_Cust_Entry100001 b ON a.fid=b.fid
                    WHERE a.fid IN({0})", string.Join(",", ids));
                    DataSet ds = DBUtils.ExecuteDataSet(Context, sql);
                    List<DynamicObject> dynamics = new List<DynamicObject>();

                    decimal areaTotal = 0;
                    int seq = 0;
                    string billNo = "";
                    try
                    {
                        //填充数据，循环所有行
                        foreach (var dynamicObject in e.DataObjects)
                        {
                            seq++;
                            DynamicObject obj = new DynamicObject(dot);
                            //string mark = "";
                            //string[] marks = dynamicObject["F_MARKS"].ToString().Split(',');
                            //for(int i = 0; i < marks.Length; i++)
                            //{
                            //    if (i == 0)
                            //    {
                            //        mark = marks[i];
                            //    }
                            //    else
                            //    {
                            //        mark = mark + Environment.NewLine + marks[i];
                            //    }
                            //}
                            obj["CTNS"] = Convert.ToInt32(obj["CTNS"]) + Convert.ToInt32(ds.Tables[0].Rows[0]["ctn"]);
                            obj["SumQty"] = Convert.ToInt32(obj["SumQty"]) + Convert.ToInt32(ds.Tables[0].Rows[0]["sum_qty"]);
                            obj["SumAccount"] = Convert.ToInt32(obj["SumAccount"]) + Convert.ToInt32(ds.Tables[0].Rows[0]["sum_account"]);
                            obj["SumNW"] = Convert.ToInt32(obj["SumNW"]) + Convert.ToInt32(ds.Tables[0].Rows[0]["nw"]);
                            obj["SumGW"] = Convert.ToInt32(obj["SumGW"]) + Convert.ToInt32(ds.Tables[0].Rows[0]["gw"]);

                            //obj["Marks"] = mark;
                            dynamics.Add(obj);
                        }
                    }
                    catch (Exception er)
                    {
                        throw new Exception(string.Format("报关单 【{0}】行号【{1}】套打数据错误，错误信息:{2}", billNo, seq, er.Message));
                    }
                    e.DataObjects = dynamics.ToArray();
                }
            }
        }
    }
}
